package cn.smxy.comment.service.impl;

import cn.smxy.client.CommentClient;
import cn.smxy.comment.mapper.CommentMapper;
import cn.smxy.comment.service.CommentService;
import cn.smxy.comment.util.CommentUtil;
import cn.smxy.dto.CommentCountDto;
import cn.smxy.param.CommentUserPage;
import cn.smxy.pojo.Comment;
import cn.smxy.util.R;
import cn.smxy.vo.CommentVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CommentUtil commentUtil;

    /**
     * 获取作品评论
     *
     * @param plantId
     * @return
     */
    @Override
    public R commentPlant(Long plantId) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plant_id",plantId);
        List<Comment> list = commentMapper.selectList(queryWrapper);
        List<CommentVo> commentVoList = commentUtil.processComments(list, true);// 返回树形结构或者二维展开
        log.info("CommentServiceImpl.commentPlant业务结束，结果为:{}","查询作品评论成功");
        return R.ok(null,commentVoList);
    }

    /**
     * 删除对应作品的评论
     *
     * @param plantId
     * @return
     */
    @Override
    public R removeByPlantId(Long plantId) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plant_id",plantId);
        int rows = commentMapper.delete(queryWrapper);
        log.info("CommentServiceImpl.removeById业务结束，结果为:{}",rows);
        return R.ok("删除成功");
    }

    /**
     * 获取各作品评论数
     *
     * @return
     */
    @Override
    public List<CommentCountDto> getCount() {
        List<CommentCountDto> countList = commentMapper.getCount();
        return countList;
    }

    /**
     * 删除对应用户的评论
     *
     * @param userId
     * @return
     */
    @Override
    public R removeById(Long userId) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        int rows = commentMapper.delete(queryWrapper);
        log.info("CommentServiceImpl.removeById业务结束，结果为:{}",rows);
        return R.ok("删除成功");
    }

    /**
     * 获取用户评论
     *
     * @param commentUserPage
     * @return
     */
    @Override
    public R commentUser(CommentUserPage commentUserPage) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",commentUserPage.getUserId());
        IPage<Comment> page = new Page<>(commentUserPage.getCurrentPage(),commentUserPage.getPageSize());
        page = commentMapper.selectPage(page, queryWrapper);
        long total = page.getTotal();
        List<Comment> records = page.getRecords();
        log.info("CommentServiceImpl.commentUser业务结束，结果为:{}","查询用户评论成功");
        return R.ok(null,records,total);
    }

    /**
     * 添加评论
     *
     * @param comment
     * @return
     */
    @Override
    public R save(Comment comment) {
        comment.setCreateTime(System.currentTimeMillis());
        int rows = commentMapper.insert(comment);
        if(rows == 0) {
            return R.fail("发表评论失败");
        }
        log.info("CommentServiceImpl.save业务结束，结果为:{}",rows);
        return R.ok("发表成功");
    }
}
